﻿using System.Web.Mvc;
using Pay.DTO;
using Pay.IService;
using Pay.TO;

namespace Pay.Web.Areas.Admin.Controllers {
	[RouteArea("admin")]
	[RoutePrefix("order")]
	public class OrderController : AdminController {
		private IOrderService _iOrderService;
        private IMerchantService _iMerchantService;

		public OrderController(IOrderService iOrderService,IMerchantService iMerchantService) {
			_iOrderService = iOrderService;
            _iMerchantService = iMerchantService;
		}

		public ActionResult Index() {
			return View();
		}

		public JsonResult Gets(OrderQuery query) {
			var model = _iOrderService.Get(query);
			return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
		}

		public JsonResult Get(long id) {
			var model = _iOrderService.Get(id);
            model.binds.ForEach(c=> {
                var member = _iMerchantService.Get(c.merchant_no);
                c.merchant_no = string.Format("手机号：{0}-姓名：{1}-商户号：{2}", member.mobile, member.name, member.merchant_no);
            });
			return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
		}

        /// <summary>
        /// 发货
        /// </summary>
        /// <returns>The save.</returns>
        /// <param name="order">Order.</param>
        public JsonResult Shipping(order_dto_ex order) {
            var result = _iOrderService.OrderDelivery(order);
            return Json(ResultToJson.Json(result > 0 ? "发货成功" : "发货失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 退款/退货
        /// </summary>
        /// <param name="order_refund"></param>
        /// <returns></returns>
        public JsonResult OrderRefund(order_refund_dto order_refund) {
            order_refund.merchant_id = currentUser.user_id;
            var result = _iOrderService.OrderRefund(order_refund);
            return Json(ResultToJson.Json(result > 0 ? "退款/退货成功" : "退款/退货失败", result), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ConfirmOrder(long id) {
            var result = _iOrderService.ConfirmOrder(id);
            return Json(ResultToJson.Json(result >= 0 ? " 确认收货成功" : "确认收货失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}